package com.easywan.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easywan.core.BaseController;
import com.easywan.pojo.IResult;
import com.easywan.pojo.IResultSupport;
import com.easywan.service.IBaseFriendLinkService;
import com.easywan.service.IBaseUsersService;
import com.easywan.service.IToolsFeedBackService;
import com.easywan.service.IToolsNameService;
import com.easywan.util.StringsUtils;
import com.easywan.vo.BaseUsers;
import com.easywan.vo.ToolsName;

@Controller
public class IndexController extends BaseController{
	Logger logger = Logger.getLogger(IndexController.class);
	@Autowired
	IBaseUsersService usersService;
	@Autowired
	IToolsFeedBackService feedBackService;
	@Autowired
	IBaseFriendLinkService friendLinkService;
	@Autowired
	IToolsNameService toolsNameService;
	
	@RequestMapping("index")
	public String index(ModelMap modelMap){
		List<ToolsName> toolsName = toolsNameService.getAll();
		List<ToolsName> toolsType = getTypeAll(toolsName);
		modelMap.addAttribute("toolsType",toolsType);
		modelMap.addAttribute("tools",toolsName);
		return "index.jsp";
	}
	private List<ToolsName> getTypeAll(List<ToolsName> toolsName) {
		List<ToolsName> rst = new ArrayList<>();
		for(ToolsName forToolsName:toolsName){
			boolean flags = true;
			for(ToolsName forRst:rst){
				if(forToolsName.getTypeId().equals(forRst.getTypeId())){
					flags = false;
					break;
				}
			}
			if(flags) rst.add(forToolsName);
		}
		return rst;
	}
	@RequestMapping("register")
	public String regiter(){
		return "register.jsp";
	}
	@RequestMapping("login")
	public String login(){
		return "login.jsp";
	}
	@ResponseBody
	@RequestMapping("toLogin")
	public String toLogin(HttpServletRequest request){
		IResult result = null;
		try{
			String loginUserName = getEmail(request,"name");
			if(loginUserName == null){
				loginUserName = getString(request, "name");
			}
			String pwd = getPwd(request);
			paramsNotNull(loginUserName,pwd);
			BaseUsers users = usersService.login(loginUserName,pwd);
			if(users == null){
				throw new RuntimeException("鐧诲綍澶辫触锛佽处鍙峰瘑鐮侀敊璇�");
			}
			setUserInfo(request, users);
			result = IResultSupport.buildResult(IResult.SUCCESS_CODE,"鐧诲綍鎴愬姛");
			IResultSupport.setModel(result, "http://tool.easywan.info/");
		}catch(Exception e){
			e.printStackTrace();
			result = IResultSupport.buildResult(IResult.FAILT_CODE,e.getMessage());
		}
		return toJSON(result);
	}
	@ResponseBody
	@RequestMapping("toRegister")
	public String toRegister(HttpServletRequest request){
		IResult result = null;
		try{
			String name = getString(request, "name");
			String pwd = getPwd(request);
			String email = getEmail(request);
			String mobile = getMobile(request);
			paramsNotNull(name,pwd,email);
			result = usersService.register(name, pwd, email,mobile);
			IResultSupport.setModel(result, "http://tool.easywan.info/");
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage());
			result = IResultSupport.buildResult(IResult.FAILT_CODE,e.getMessage());
		}
		return toJSON(result);
	}
	
	@ResponseBody
	@RequestMapping(value = "fankui",produces="text/html;charset=UTF-8")
	public String fankui(HttpServletRequest request,HttpServletResponse response){
		try{
		String name = getString(request,"name");
		String tel = getString(request, "tel");
		
		if(StringsUtils.isBank(name)){
			name = "娓稿";
		}
		String msg = getString(request,"msg");
		paramsNotNull(msg);
		
		feedBackService.insert(name, tel, msg);
		
		}catch(Exception e){
			e.printStackTrace();
			return toJSON(IResultSupport.buildResult(IResult.FAILT_CODE, ""));
		}
		return toJSON(IResultSupport.buildResult(IResult.SUCCESS_CODE, ""));
	}
	@ResponseBody
	@RequestMapping("getFriendLink")
	public String getFriendLink(HttpServletRequest rquest){
		IResult result = null; 
		result = friendLinkService.getAll();
		return toJSON(result);
	}
	
}
